package cn.wit.testalgorithms;

/**
 * 13.罗马数字转整数
 * 
 * @author 16604
 *
 */
public class DemoRomanNumberToInteger {
	public static void main(String[] args) {

		String s = "MCMXCIV";
		System.out.println(romanToInt(s));

	}

	public static int romanToInt(String s) {
		
		//边界问题
		if(s.length()==0) {
			return 0;
		}

		// 用来记录数值
		int count = 0;

		// 先将每个字符加起来再说
		for (int i = 0; i < s.length(); i++) {

			switch (s.charAt(i)) {
			case 'I':
				count = count + 1;
				break;
			case 'V':
				count = count + 5;
				break;
			case 'X':
				count = count + 10;
				break;
			case 'L':
				count = count + 50;
				break;
			case 'C':
				count = count + 100;
				break;
			case 'D':
				count = count + 500;
				break;
			case 'M':
				count = count + 1000;
				break;
			}
		}
		
		//解决特殊问题
		for(int i=0;i<s.length()-1;i++) {
			if(s.charAt(i)=='I'&&(s.charAt(i+1)=='V'||s.charAt(i+1)=='X')) {
//				System.out.println(1);
				count = count -2;
			}
			if(s.charAt(i)=='X'&&(s.charAt(i+1)=='L'||s.charAt(i+1)=='C')) {
//				System.out.println(1);
				count = count -20;
			}
			if(s.charAt(i)=='C'&&(s.charAt(i+1)=='D'||s.charAt(i+1)=='M')) {
//				System.out.println(1);
				count = count -200;
			}
		}
		
//		for(int i=0;i<s.length()-1;i++) {
//			if(s.charAt(i)=='X'&&(s.charAt(i+1)=='L'||s.charAt(i+1)=='C')) {
////				System.out.println(1);
//				count = count -20;
//			}
//		}
//		
//		for(int i=0;i<s.length()-1;i++) {
//			if(s.charAt(i)=='C'&&(s.charAt(i+1)=='D'||s.charAt(i+1)=='M')) {
////				System.out.println(1);
//				count = count -200;
//			}
//		}
		
//		System.out.println(count);
		return count;
	}
}
